<?php

use app\assets\EchartsAsset;
use app\assets\TableAsset;
use app\models\AgentPackage;
use app\models\CardAttr;
use app\models\CardAttrValue;
use app\models\CardDailyUsage;
use app\models\KeyMap;
use app\models\UserPackage;
use yii\db\Expression;
use yii\helpers\Html;

/**
 * @var $this yii\web\View
 * @var $card app\models\Card
 */

EchartsAsset::register($this);
TableAsset::register($this);

$this->title = '卡详情';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<table class="table table-striped table-bordered table-hover">
    <tr>
        <th>编号</th>
        <td><?php echo $card->id;?></td>
    </tr>
    <tr>
        <th>ICCID</th>
        <td><?php echo $card->iccid;?></td>
    </tr>
    <tr>
        <th>MSISDN</th>
        <td><?php echo $card->msisdn;?></td>
    </tr>
    <tr>
        <th>IMSI</th>
        <td><?php echo $card->imsi;?></td>
    </tr>
        <th>用户</th>
        <td><?php if (empty($card->uid)) {?>
                <i>未分配</i>
            <?php } else {
                echo Html::a(Html::encode($card->user->username), ['/agent/user/user-view', 'id' => $card->uid]);
            }?></td>
    </tr>
    <tr>
        <th>套餐</th>
        <td><?php echo Html::encode($card->package->name);
            if (!empty($card->agent_id)) {
                $agentPackage = AgentPackage::findOne(['agent_id' => $card->agent_id, 'package_id' => $card->package_id]);
                if (!empty($agentPackage)) {
                    echo '（成本价：', $agentPackage->cost_price, '）';
                    echo '（销售价：', $agentPackage->sale_price, '）';
                } else {
                    echo '（价格未设置）';
                }
            }
            if (!empty($card->uid)) {
                $userPackage = UserPackage::findOne(['uid' => $card->uid, 'package_id' => $card->package_id]);
                if (!empty($userPackage)) {
                    echo '（用户价格：', $userPackage->cost_price, '）';
                } else {
                    echo '（用户价格未设置）';
                }
            }
            echo '<br />';
            echo '数据：', Yii::$app->formatter->asShortSize($card->package->data), '，超量：', $card->package->extra_data_price, '元/', Yii::$app->formatter->asShortSize($card->package->extra_data), '<br />';
            echo '短信：', $card->package->sms, '条，超量：', $card->package->extra_sms_price, '元/条<br />';
            echo '语音：', $card->package->voice, '分钟，超量：', $card->package->extra_voice_price, '元/分钟<br />';?>
        </td>
    </tr>
    <tr>
        <th>套餐有效期</th>
        <td><?php if (empty($card->term_length_unit)) {
                echo '<i>未设置</i>';
            } else {
                echo $card->term_length, '(', KeyMap::getValue('card_term_length_unit', $card->term_length_unit), ') ';
                if (empty($card->active_time)) {
                    echo '----';
                } else {
                    echo Yii::$app->formatter->asDate($card->active_time);
                }
                echo ' 至 ';
                if (empty($card->term_end_time)) {
                    echo '----';
                } else {
                    echo Yii::$app->formatter->asDate($card->term_end_time);
                }
            }?></td>
    </tr>
    <tr>
        <th>状态</th>
        <td><?php echo KeyMap::getValue('card_status', $card->status);?></td>
    </tr>
    <tr>
        <th>创建时间</th>
        <td><?php echo Yii::$app->formatter->asDatetime($card->create_time);?></td>
    </tr>
    <tr>
        <th>批次号</th>
        <td><?php echo $card->batch_no;?></td>
    </tr>
    <tr>
        <th>激活时间</th>
        <td><?php echo Yii::$app->formatter->asDatetime($card->active_time);?></td>
    </tr>
    <tr>
        <th>刷新时间</th>
        <td><?php echo Yii::$app->formatter->asDatetime($card->refresh_time);?></td>
    </tr>
    <tr>
        <th>备注</th>
        <td><?php echo Html::encode($card->remark);?></td>
    </tr>
</table>
<table class="table table-striped table-bordered table-hover">
    <tr>
        <th colspan="3"><h4>用量信息（本月）</h4></th>
    </tr>
    <tr>
        <th>数据</th>
        <td><?php echo Yii::$app->formatter->asShortSize($card->data);?></td>
        <td><?php $dataPercent = $card->package->data == 0 ? 100 : $card->data * 100 / $card->package->data;?>
            <div class="progress" style="margin-bottom: 3px; min-width: 200px;">
                <div class="progress-bar progress-bar-<?php echo $dataPercent < 80 ? 'success' : ($dataPercent < 100 ? 'warning' : 'danger');?> progress-bar-striped" role="progressbar" aria-valuenow="<?php echo $card->data;?>" aria-valuemin="0" aria-valuemax="<?php echo $card->package->data;?>" style="width: <?php echo $dataPercent;?>%">
                    <?php echo Yii::$app->formatter->asShortSize($card->data), ' / ', Yii::$app->formatter->asShortSize($card->package->data), ' ', $dataPercent, '%';?>
                </div>
            </div>
        </td>
    </tr>
    <tr>
        <th>短信</th>
        <td><?php echo $card->sms;?>条</td>
        <td><?php $smsPercent = $card->package->sms == 0 ? 100 : $card->sms * 100 / $card->package->sms;?>
            <div class="progress" style="margin-bottom: 3px; min-width: 200px;">
                <div class="progress-bar progress-bar-<?php echo $smsPercent < 80 ? 'success' : ($smsPercent < 100 ? 'warning' : 'danger');?> progress-bar-striped" role="progressbar" aria-valuenow="<?php echo $card->sms;?>" aria-valuemin="0" aria-valuemax="<?php echo $card->package->sms;?>" style="width: <?php echo $smsPercent;?>%">
                    <?php echo $card->sms, ' / ', $card->package->sms, ' ', $smsPercent, '%';?>
                </div>
            </div>
        </td>
    </tr>
    <tr>
        <th>语音</th>
        <td><?php echo $card->voice;?>分钟</td>
        <td><?php $voicePercent = $card->package->voice == 0 ? 100 : $card->voice * 100 / $card->package->voice;?>
            <div class="progress" style="margin-bottom: 3px; min-width: 200px;">
                <div class="progress-bar progress-bar-<?php echo $voicePercent < 80 ? 'success' : ($voicePercent < 100 ? 'warning' : 'danger');?> progress-bar-striped" role="progressbar" aria-valuenow="<?php echo $card->voice;?>" aria-valuemin="0" aria-valuemax="<?php echo $card->package->voice;?>" style="width: <?php echo $voicePercent;?>%">
                    <?php echo $card->voice, ' / ', $card->package->voice, ' ', $voicePercent, '%';?>
                </div>
            </div>
        </td>
    </tr>
</table>

<table class="table table-striped table-bordered">
    <tr>
        <td height="200" id="chart_card_usage"></td>
    </tr>
</table>
<script>
    function showChartCardUsage() {
        <?php $usageList = CardDailyUsage::find()
        ->select([
            'date' => 'date',
            'data' => new Expression('CAST(data / 1024 / 1024 AS DECIMAL(20, 4))'),
        ])
        ->asArray()
        ->andWhere(['card_id' => $card->id])
        ->andWhere(['>=', 'date', strtotime('Ymd', time() - 30 * 86400)])
        ->all();
        $dateList = array_column($usageList, 'date');
        $dataList = array_column($usageList, 'data');?>
        var dateList = <?php echo json_encode($dateList);?>;
        var dataList = <?php echo jsoN_encode($dataList);?>;
        var chart = echarts.init(document.getElementById('chart_card_usage'), 'macarons');
        chart.setOption({
            title: {
                text: '用量统计（单位MB）：'
            },
            tooltip : {
                trigger: 'axis',
                axisPointer : {
                    type : 'shadow'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis : [
                {
                    type : 'category',
                    data : dateList,
                    axisTick: {
                        alignWithLabel: true
                    }
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : [
                {
                    name:'流量',
                    type:'bar',
                    barWidth: '60%',
                    data:dataList
                }
            ]
        });
    }
    <?php $this->registerJs('showChartCardUsage();');?>
</script>
<?php $dataMoney = $card->data <= $card->package->data ? 0 : $card->package->extra_data_price * ($card->data - $card->package->data) / $card->package->extra_data;
$smsMoney = $card->sms <= $card->package->sms ? 0 : $card->package->extra_sms_price * ($card->sms - $card->package->sms);
$voiceMoney = $card->voice <= $card->package->voice ? 0 : $card->package->extra_voice_price * ($card->voice - $card->package->voice);?>
<table class="table table-striped table-bordered table-hover">
    <tr>
        <th colspan="6"><h4>费用信息（本月）</h4></th>
    </tr>
    <tr>
        <th>数据</th>
        <td><?php echo $dataMoney;?>元</td>
        <th>短信</th>
        <td><?php echo $smsMoney;?>元</td>
        <th>语音</th>
        <td><?php echo $voiceMoney;?>元</td>
    </tr>
    <tr>
        <th>基本费用</th>
        <td><?php $agentPackage = AgentPackage::findOne(['agent_id' => $card->agent_id, 'package_id' => $card->package_id]);
            echo $agentPackage->cost_price;?>元</td>
        <th>合计</th>
        <td><?php echo ($agentPackage->cost_price + $dataMoney + $smsMoney + $voiceMoney);?>元</td>
        <th></th>
        <td></td>
    </tr>
</table>
<?php if (!empty($card->uid)) {?>
    <table class="table table-striped table-bordered table-hover">
        <tr>
            <th colspan="4"><h4>用户自定义属性信息</h4></th>
        </tr>
        <?php /** @var CardAttr $attr */
        foreach (CardAttr::find()->andWhere(['uid' => $card->uid, 'status' => CardAttr::STATUS_OK])->orderBy('sort DESC, id ASC')->each() as $attr) {?>
            <tr>
                <th><?php echo Html::encode($attr->name);?></th>
                <td><pre><?php $attrValue = CardAttrValue::findOne(['uid' => $card->uid, 'attr_id' => $attr->id, 'card_id' => $card->id]);
                        if (!empty($attrValue)) {
                            echo Html::encode($attr->getHtml($attrValue->value));
                        }?>
                    </pre>
                </td>
            </tr>
        <?php }?>
    </table>
<?php }?>
